package com.site.dao.impl;

import com.site.dao.ReserveDao;
import com.site.pojo.Reserve;
import com.site.utils.JDBCUtils;
import jdk.nashorn.internal.scripts.JD;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * FileName: ReserveDaoImpl.java
 * 货架表DaoImpl
 *
 * @author 田尚忠
 * @Date    2021/11/28
 * @version 1.00
 */
public class ReserveDaoImpl implements ReserveDao {

    @Override
    public List<Reserve> selectAll() throws SQLException {
        List<Reserve> list = new ArrayList<>();
        Connection conn = JDBCUtils.getConnection();
        String sql = "SELECT `quantity`, `rid`, `goods_id`, `rtid`, `neednum`, `amount`, `state` FROM `reserve`";
        ResultSet rs = JDBCUtils.executeQuery(conn, sql);
        while (rs.next()){
            Reserve reserve = new Reserve();
            reserve.setQuantity(rs.getLong("quantity"));
            reserve.setRid(rs.getLong("rid"));
            reserve.setGoodsId(rs.getLong("goods_id"));
            reserve.setRtid(rs.getLong("rtid"));
            reserve.setNeednum(rs.getLong("neednum"));
            reserve.setAmount(rs.getLong("amount"));
            reserve.setState(rs.getLong("state"));
            list.add(reserve);
        }
        JDBCUtils.close(rs,null,conn);
        return list;
    }

    @Override
    public Reserve selectByid(int id) throws SQLException {
        Reserve reserve = new Reserve();
        Connection conn = JDBCUtils.getConnection();
        String sql = "SELECT `quantity`, `rid`, `goods_id`, `rtid`, `neednum`, `amount`, `state` FROM `reserve` where rid=?";
        ResultSet rs = JDBCUtils.executeQuery(conn, sql,id);
        while (rs.next()){
            reserve.setQuantity(rs.getLong("quantity"));
            reserve.setRid(rs.getLong("rid"));
            reserve.setGoodsId(rs.getLong("goods_id"));
            reserve.setRtid(rs.getLong("rtid"));
            reserve.setNeednum(rs.getLong("neednum"));
            reserve.setAmount(rs.getLong("amount"));
            reserve.setState(rs.getLong("state"));
        }
        JDBCUtils.close(rs,null,conn);
        return reserve;
    }

    @Override
    public List<Reserve> selectByGoodsId(int id) throws SQLException {
        List<Reserve> list = new ArrayList<>();
        Connection conn = JDBCUtils.getConnection();
        String sql = "SELECT `quantity`, `rid`, `goods_id`, `rtid`, `neednum`, `amount`, `state` FROM `reserve` where goods_id=?";
        ResultSet rs = JDBCUtils.executeQuery(conn, sql,id);
        while (rs.next()){
            Reserve reserve = new Reserve();
            reserve.setQuantity(rs.getLong("quantity"));
            reserve.setRid(rs.getLong("rid"));
            reserve.setGoodsId(rs.getLong("goods_id"));
            reserve.setRtid(rs.getLong("rtid"));
            reserve.setNeednum(rs.getLong("neednum"));
            reserve.setAmount(rs.getLong("amount"));
            reserve.setState(rs.getLong("state"));
            list.add(reserve);
        }
        JDBCUtils.close(rs,null,conn);
        return list;
    }

    @Override
    public List<Reserve> selectByState(int state) throws SQLException {
        List<Reserve> list = new ArrayList<>();
        Connection conn = JDBCUtils.getConnection();
        String sql = "SELECT `quantity`, `rid`, `goods_id`, `rtid`, `neednum`, `amount`, `state` FROM `reserve` where state=?";
        ResultSet rs = JDBCUtils.executeQuery(conn, sql,state);
        while (rs.next()){
            Reserve reserve = new Reserve();
            reserve.setQuantity(rs.getLong("quantity"));
            reserve.setRid(rs.getLong("rid"));
            reserve.setGoodsId(rs.getLong("goods_id"));
            reserve.setRtid(rs.getLong("rtid"));
            reserve.setNeednum(rs.getLong("neednum"));
            reserve.setAmount(rs.getLong("amount"));
            reserve.setState(rs.getLong("state"));
            list.add(reserve);
        }
        JDBCUtils.close(rs,null,conn);
        return list;
    }

    @Override
    public int addReserve(Reserve reserve) {
        String sql = "INSERT INTO `hurricane_house`.`reserve` (`quantity`, `rid`, `goods_id`, `rtid`, `neednum`, `amount`, `state`) VALUES (0,?,?,4,10000, 10000, 1);";
        return JDBCUtils.executeUpdate(sql,new Object[]{reserve.getRid(),reserve.getGoodsId()});
    }

    @Override
    public int addReserve(Connection conn, Reserve reserve) {
        String sql = "INSERT INTO `hurricane_house`.`reserve` (`quantity`, `rid`, `goods_id`, `rtid`, `neednum`, `amount`, `state`) VALUES (0,?,?,4,10000, 10000, 1);";
        return JDBCUtils.executeUpdate(sql,new Object[]{reserve.getRid(),reserve.getGoodsId()});
    }

    @Override
    public int delByrid(int id) {
        String sql = "DELETE FROM reserve WHERE rid=?";
        return JDBCUtils.executeUpdate(sql,new Object[]{id});
    }

    @Override
    public int delByrid(Connection conn, int id) {
        String sql = "DELETE FROM reserve WHERE rid=?";
        return JDBCUtils.executeUpdate(conn,sql,new Object[]{id});
    }

    @Override
    public int delByState(int state) {
        String sql = "DELETE FROM reserve WHERE state=?";
        return JDBCUtils.executeUpdate(sql,new Object[]{state});
    }

    @Override
    public int delByState(Connection conn, int state) {
        String sql = "DELETE FROM reserve WHERE state=?";
        return JDBCUtils.executeUpdate(conn,sql,new Object[]{state});
    }

    @Override
    public int updateReserve(int id,Reserve reserve) {
        String sql = "UPDATE `hurricane_house`.`reserve` SET `quantity` = ?, `goods_id` = ?, `rtid` = ?, `neednum` = ?, `amount` = ?, `state` = ? WHERE `rid` = ?;\n";
        Object[] params = {reserve.getQuantity(),reserve.getGoodsId(), reserve.getRtid(), reserve.getNeednum(), reserve.getAmount(), reserve.getState(),id};
        return JDBCUtils.executeUpdate(sql,params);
    }

    @Override
    public int updateReserve(Connection conn, int id,Reserve reserve) {
        String sql = "UPDATE `hurricane_house`.`reserve` SET `quantity` = ?, `goods_id` = ?, `rtid` = ?, `neednum` = ?, `amount` = ?, `state` = ? WHERE `rid` = ?;\n";
        Object[] params = {reserve.getQuantity(),reserve.getGoodsId(), reserve.getRtid(), reserve.getNeednum(), reserve.getAmount(), reserve.getState(),id};
        return JDBCUtils.executeUpdate(sql,params);
    }
}
